﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using Newtonsoft.Json;
using log4net;
using System.Reflection;
using System.Data;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 培训类型，如文档、视频、考试、练习等
    /// </summary>
    public class TrainTypeController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


        // POST: api/TrainType
        /// <summary>
        /// 培训类型增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainType/Add")]
        public IHttpActionResult Add(Model.TrainType model)
        {
            log.Info(JsonConvert.SerializeObject(model));
            BLL.TrainType bllTrainType = new BLL.TrainType();
            ResultMsg resultMsg = new ResultMsg(); 
            try
            {
                if (!bllTrainType.Exists(model.TTID))
                {
                    if (bllTrainType.Add(model) > 0)
                    {
                        //返回token信息
                        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                        resultMsg.Data = "培训类型增加成功！";
                    }
                    else
                    {
                        //返回token信息
                        resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                        resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                        resultMsg.Data = "培训类型增加失败！";
                    }
                }
                else
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "培训类型已经存在，请不要重复！";
                }
            }
            catch (Exception exc)
            {

                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "培训类型增加异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        // POST: api/Employee
        /// <summary>
        /// 培训类型分页查找
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainType/Search")]
        public IHttpActionResult Search(string swhere, int page, int limit)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;

                if (!string.IsNullOrEmpty(swhere == null ? string.Empty : swhere.Trim()))
                {
                    Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                    swhere = string.Empty;
                    swhere = " 1=1 ";
                    foreach (var item in jsonObject)
                    {
                        //if (item.Key == "EmpID")
                        //{
                        //    swhere += $" and EmpID like '%{item.Value}%'";
                        //}
                        if (item.Key == "TTName")
                        {
                            swhere += $" and TTName like '%{item.Value}%'";
                        }
                    }
                }
                int rowCount = 0;
                bool blExist = false;
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainType().GetListByPage(swhere, "TTID", startindex, endindex,out rowCount);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            resultMsg.Count = rowCount;
                            blExist = true;
                        }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.NoData;
                    resultMsg.Info = StatusCodeEnum.NoData.GetEnumText();
                    resultMsg.Data = "查询结果不存在！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 培训类型编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainType/Edit")]
        public IHttpActionResult Edit(Model.TrainType model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainType().Update(model))
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "培训类型编辑成功！";
                }
                else
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "培训类型编辑失败！";
                }
            }
            catch (Exception exc)
            {

                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "培训类型编辑时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 培训类型删除(根据编号)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainType/Del")]
        public IHttpActionResult Del(Model.TrainType model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainType().Delete(model.TTID))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "培训类型删除成功！";
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "培训类型删除失败！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "培训类型删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 培训类型删除(根据编号)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainType/DelList")]
        public IHttpActionResult DelList(string TTIDlist)
        {
            log.Info(JsonConvert.SerializeObject(TTIDlist));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainType().DeleteList(TTIDlist))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "培训类型删除成功！";
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "培训类型删除失败！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "培训类型删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        // GET: api/TrainType/5
        public IHttpActionResult Get()
        {
            //ResponseMsg resultMsg = new ResponseMsg();
            //DataSet ds = new BLL.TrainType().GetList("");
            //resultMsg.code = (int)StatusCodeEnum.Success;
            //resultMsg.msg = StatusCodeEnum.Success.GetEnumText();
            //resultMsg.count = ds.Tables[0].Rows.Count;
            //resultMsg.data = ds.Tables[0];
            //return Json(resultMsg);
            ResultMsg resultMsg = new ResultMsg();
            DataSet ds = new BLL.TrainType().GetList("");
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
            resultMsg.Count = ds.Tables[0].Rows.Count;
            resultMsg.Data = ds.Tables[0];
            return Json(resultMsg);
        }

        public class ResponseMsg
        {
            //"code":0,"msg":"","count":1000,"data":
            /// <summary>
            /// 状态码
            /// </summary>
            public int code { get; set; }

            /// <summary>
            /// 操作信息
            /// </summary>
            public string msg { get; set; }

            /// <summary>
            /// 结果行数
            /// </summary>
            public int count { get; set; }
            /// <summary>
            /// 返回数据
            /// </summary>
            public object data { get; set; }
        }
        // GET: api/TrainType/5
        public string Get(int TTID)
        {
            Model.TrainType mtt = new Model.TrainType();
            mtt = new BLL.TrainType().GetModel(TTID);
            return JsonConvert.SerializeObject(mtt);
             
        }

        // POST: api/TrainType
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/TrainType/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/TrainType/5
        public void Delete(int id)
        {
        }
    }
}
